{extend name="common/base" /}

{block name="body"}
<script type="text/javascript" src="http://tajs.qq.com/stats?sId=65871947" charset="UTF-8"></script>
<div class="search-block">

    <form class="layui-form" action="">

        <div class="layui-container">

            <div class="layui-row">

                <div class="layui-col-md8">

                    <div class="layui-form-item">
                             <label class="layui-form-label">文库地址</label>

                        <div class="layui-input-block">

                            <input type="text" name="wenku_url" required lay-verify="required"

                                   placeholder="例如：https://wenku.baidu.com/view/1d694bbb82d049649b6648d7c1c708a1284a0ae6.html"

                                   autocomplete="off" class="layui-input">

                        </div>

                    </div>

                </div>

                <div class="layui-col-md3">

                    <div class="layui-form-item" style="margin-top: 5px;margin-left: 10px;">

                        <div class="layui-inline">

                            <div class="layui-input-inline">

                                <button class="layui-btn layui-btn-sm" id="jiexi_btn">自动代下载</button>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </form>

</div>



<div class="layui-tab-content" style="background-color: rgba(255,255,255,1);margin-top: 5px;">

    <div class="layui-tab-item layui-show">

        <blockquote class="layui-elem-quote layui-quote-nm">{$number}。历史下载记录 注：可删除、一旦删除下次下载就会消耗一次下载机会。【半个小时左右点击下载有效】

        </blockquote>

        <div class="layui-btn-group demoTable" style="float: left;">

            <button class="layui-btn layui-btn-sm" id="delete">批量删除</button>

        </div>

        <span class="layui-clear"></span>

        <table class="layui-hide" id="table_list" lay-filter="lists"></table>

    </div>

</div>

<script type="text/html" id="tool">

    <a class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius " lay-event="down_url">下载</a>

    <a class="layui-btn layui-btn-primary layui-btn-xs layui-btn-radius" lay-event="delete">删除</a>

</script>



<script type="text/html" id="ismenu-tpl">

    <input type="checkbox" name="ismenu" data-id="{{ d.id }}" value="{{d.ismenu}}" lay-skin="switch" lay-text="是|否"

           lay-filter="is_menu" {{ d.ismenu== 1 ? 'checked' : '' }}>

</script>

<script type="text/html" id="icon">

    <div style="text-align: center">

        <i class="fa {{d.icon}}" style="font-size: 20px;"></i>

    </div>

</script>

{/block}

{block name="script"}

<script type="text/javascript">

    var table;

    layui.config({

        base: '__ADMIN__/modules/'

    }).use(['layer', 'element', 'table', 'jquery', 'laydate', 'home', 'form'], function () {

        var layer = layui.layer,

            $ = layui.jquery,

            laydate = layui.laydate,

            form = layui.form,

            home = layui.home,

            element = layui.element;

        table = layui.table;

        //日期

        laydate.render({

            elem: '#create_time'

            , range: true

        });

        //保存当前页

        var page = 1;

        var limit = home.fitPage();

        //方法级渲染

        table.render({

            elem: '#table_list'

            , url: "{:url('Wenku/lists')}"

            , cols: [[

                {checkbox: true, fixed: true}

                , {field: 'id', title: 'ID', width: "5%"}

                , {field: 'before_url', title: '原地址', width: "35%"}

                , {field: 'after_url', title: '下载地址', width: "35%", edit: "text"}

                , {field: 'created_time', title: '解析时间', width: "15%"}

                , {fixed: 'right', width: "10%", align: 'center', toolbar: '#tool'}

            ]]

            , id: 'lists'

            , page: true

            , limit: limit

            , response: {

                statusName: 'code' //数据状态的字段名称，默认：code

                , statusCode: 200 //成功的状态码，默认：0

                , msgName: 'msg' //状态信息的字段名称，默认：msg

                , countName: 'count' //数据总数的字段名称，默认：count

                , dataName: 'data' //数据列表的字段名称，默认：data

            }, done: function (res, curr, count) {

                page = curr;

            }

        });

        //监听单元格编辑

        table.on('edit(lists)', function (obj) {

            var value = obj.value //得到修改后的值

                , data = obj.data //得到所在行所有键值

                , field = obj.field; //得到字段

            $.ajax({

                type: "POST",

                async: true,

                url: "{:url('Menus/modify')}",

                data: data,

                dataType: "json",

                success: function (res) {

                    if (res && res['status'] == 200) {

                        layer.msg(res.msg, {icon: 1});

                    } else {

                        layer.msg(res.msg, {icon: 2});

                    }

                }

            });

        });

        $("#jiexi_btn").click(function () {

            var wenku_url = $("input[name='wenku_url']").val();

            $.ajax({

                type: "POST",

                async: true,

                url: "{:url('Wenku/jiexi')}",

                data: {scurl: wenku_url},

                dataType: "json",

                success: function (res) {

                    if (res && res['status'] == 200) {

                        layer.msg(res.msg, {icon: 1}, function () {

                            window.location.href = res['data'];

                            // Handle the complete event

                            //执行重载

                            table.reload('lists', {

                                page: {

                                    curr: page //重新从第 1 页开始

                                }

                            });

                            layer.closeAll();

                        });

                    } else {

                        layer.msg(res.msg, {icon: 2},function () {

                            layer.closeAll();
                            if (res.msg == '您的账户已过期、请充值！') {
                                window.location.href = "{:url('account/lists')}";
                            }else if (res.msg == '亲，今日已达到下载上限！。。。。。。') {
                                window.location.href = "{:url('Login/index')}";
                            }
                        });

                    }

                },

                beforeSend: function () {

                    // Handle the beforeSend event

                    var index = layer.load(1, {

                        shade: [0.1, '#ccc'] //0.1透明度的白色背景

                    });

                },

                complete: function () {

                }

            });

            return false;

        });

        //监听工具条

        table.on('tool(lists)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"

            var data = obj.data; //获得当前行数据

            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            var tr = obj.tr; //获得当前行 tr 的DOM对象



            if (layEvent === 'down_url') { //编辑

                window.open(data['after_url']);

            } else if (layEvent === 'delete') { //删除

                layer.confirm('数据无价、请谨慎操作？', function (index) {

                    $.ajax({

                        type: "post",

                        async: true,

                        url: "{:url('Wenku/delete')}",

                        data: {id: data.id},

                        dataType: "json",

                        success: function (res) {

                            if (res && res['status'] == 200) {

                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存

                                layer.close(index);

                            } else {

                                layer.msg(res.msg, {icon: 2});

                            }

                        }

                    });

                });

            } else if (layEvent === 'create-children') {

                layer.open({

                    title: "添加" + data.title + "的子菜单菜单",

                    type: 2,

                    area: ['400px', '400px'],

                    content: "{:url('Menus/created')}" + "?pid=" + data.id,

                    success: function (layero, index) {

                        console.log(layero, index);

                    }

                });

            }

        });

        form.on('switch(is_menu)', function (obj) {

            var ismenu = obj.elem.checked ? 1 : 2;

            var id = $(obj.elem).attr("data-id");

            var data = {"ismenu": ismenu, "id": id};

            $.ajax({

                type: "POST",

                async: true,

                url: "{:url('Menus/modify')}",

                data: data,

                dataType: "json",

                success: function (res) {

                    if (res && res['status'] == 200) {

                        layer.msg(res.msg, {icon: 1});

                    } else {

                        layer.msg(res.msg, {icon: 2});

                    }

                }

            });

        });

        //添加

        $("#created").click(function () {

            layer.open({

                title: "创建菜单",

                type: 2,

                area: ['400px', '400px'],

                content: "{:url('Menus/created')}"

            });

        });

        //全选删除

        $("#delete").click(function () {

            var checkStatus = table.checkStatus('lists')

                , data = checkStatus.data;

            var ids = "";

            $.each(data, function (x, y) {

                ids += "," + y.id;

            })

            layer.confirm('数据无价、请谨慎操作？', function (index) {

                $.ajax({

                    type: "post",

                    async: true,

                    url: "{:url('Wenku/delete')}",

                    data: {id: ids},

                    dataType: "json",

                    success: function (res) {

                        if (res && res['status'] == 200) {

                            table.reload('lists', {

                                page: {

                                    curr: page

                                }

                            });

                            layer.close(index);

                        } else {

                            layer.msg(res.msg, {icon: 2});

                        }

                    }

                });

            });

        })

    });

</script>

{/block}